package cn.mcch.investigator.presenter.impl;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.mcch.investigator.R;
import cn.mcch.investigator.adapter.CarTypeExpandableListAdapter;
import cn.mcch.investigator.application.InvestigatorApplication;
import cn.mcch.investigator.customview.IndexExpandableListView;
import cn.mcch.investigator.listener.OnResponseListener;
import cn.mcch.investigator.model.bean.CarType;
import cn.mcch.investigator.model.bean.CarTypeBean;
import cn.mcch.investigator.model.impl.ChooseCarTypeModelImpl;
import cn.mcch.investigator.presenter.BasePresenter;
import cn.mcch.investigator.presenter.IChooseCarTypePresenter;
import cn.mcch.investigator.utils.ConstantUtil;
import cn.mcch.investigator.utils.JsonUtils;
import cn.mcch.investigator.viewdelegate.impl.ChooseCarTypeViewDelegateImpl;

/**
 * Created by Administrator on 2016/12/20.
 */
public class ChooseCarTypePresenterImpl extends BasePresenter<ChooseCarTypeViewDelegateImpl, ChooseCarTypeModelImpl> implements IChooseCarTypePresenter {
    public CarTypeBean carTypeBean;
    public List<List<CarType>> childDatas;

    @Override
    public Class<ChooseCarTypeViewDelegateImpl> getViewDelegateClass() {
        return ChooseCarTypeViewDelegateImpl.class;
    }

    @Override
    public Class<ChooseCarTypeModelImpl> getModelClass() {
        return ChooseCarTypeModelImpl.class;
    }

    @Override
    public void viewBindData(ChooseCarTypeViewDelegateImpl viewDelegate, String json) {

        carTypeBean = JsonUtils.fromJsonToObject(json, CarTypeBean.class);
        List<String> groupDatas = new ArrayList<>();
        childDatas = new ArrayList<>();
        String group = "";
        List<CarType> childList = null;
        for (CarType carList : carTypeBean.list) {
            if (!group.equals(carList.letter)) {
                if (childList != null) {
                    childDatas.add(childList);
                }
                group = carList.letter;
                groupDatas.add(carList.letter);
                childList = new ArrayList<>();

            }
            childList.add(carList);
        }
        // 添加最后一组数据
        childDatas.add(childList);
        CarTypeExpandableListAdapter adapter = new CarTypeExpandableListAdapter(InvestigatorApplication.getInstance(), groupDatas, childDatas, R.layout.list_item_car_group, R.layout.list_item_car_child);
        ((IndexExpandableListView) viewDelegate.findViewById(R.id.iel_car_type)).setIndex(groupDatas).setHeader(carTypeBean.hot).setAdapter(adapter);
    }


    @Override
    public void getCarType2(int requestId, String id, OnResponseListener l) {
        Map<String, String> params = new LinkedHashMap<>();
        params.put("id", id);
        mModel.request(requestId, ConstantUtil.ACT_CAR_TYPE_CHILD, params, l);
    }
}
